// 全局变量和常量
const API_BASE_URL = '/api';

// DOM 元素引用
const dashboardView = document.getElementById('dashboardView');
const usersView = document.getElementById('usersView');
const deliveryPersonsView = document.getElementById('deliveryPersonsView');
const ordersView = document.getElementById('ordersView');
const statisticsView = document.getElementById('statisticsView');

const dashboardLink = document.getElementById('dashboardLink');
const usersLink = document.getElementById('usersLink');
const deliveryPersonsLink = document.getElementById('deliveryPersonsLink');
const ordersLink = document.getElementById('ordersLink');
const statisticsLink = document.getElementById('statisticsLink');
const logoutBtn = document.getElementById('logoutBtn');

const userCountElement = document.getElementById('userCount');
const deliveryPersonCountElement = document.getElementById('deliveryPersonCount');
const orderCountElement = document.getElementById('orderCount');
const completedOrderCountElement = document.getElementById('completedOrderCount');

const recentOrdersTable = document.getElementById('recentOrdersTable');
const deliveryPersonsTable = document.getElementById('deliveryPersonsTable');

const usersTable = document.getElementById('usersTable');
const deliveryPersonsFullTable = document.getElementById('deliveryPersonsFullTable');
const ordersTable = document.getElementById('ordersTable');

const refreshDashboardBtn = document.getElementById('refreshDashboardBtn');
const refreshUsersBtn = document.getElementById('refreshUsersBtn');
const refreshDeliveryPersonsBtn = document.getElementById('refreshDeliveryPersonsBtn');
const refreshOrdersBtn = document.getElementById('refreshOrdersBtn');
const refreshStatisticsBtn = document.getElementById('refreshStatisticsBtn');

// 初始化仪表盘
function initDashboard() {
    // 检查管理员身份验证
    checkAdminAuth();
    
    // 加载仪表盘数据
    loadDashboardData();
    
    // 设置导航链接事件监听器
    dashboardLink.addEventListener('click', showDashboardView);
    usersLink.addEventListener('click', showUsersView);
    deliveryPersonsLink.addEventListener('click', showDeliveryPersonsView);
    ordersLink.addEventListener('click', showOrdersView);
    statisticsLink.addEventListener('click', showStatisticsView);
    logoutBtn.addEventListener('click', logout);
    
    // 设置刷新按钮事件监听器
    refreshDashboardBtn.addEventListener('click', loadDashboardData);
    refreshUsersBtn.addEventListener('click', loadUsersData);
    refreshDeliveryPersonsBtn.addEventListener('click', loadDeliveryPersonsData);
    refreshOrdersBtn.addEventListener('click', loadOrdersData);
    refreshStatisticsBtn.addEventListener('click', loadStatisticsData);
}

// 检查管理员身份验证
function checkAdminAuth() {
    // 临时返回 true，实际实现应该检查用户是否已登录并具有管理员权限
    return true;
}

// 隐藏所有视图
function hideAllViews() {
    dashboardView.style.display = 'none';
    usersView.style.display = 'none';
    deliveryPersonsView.style.display = 'none';
    ordersView.style.display = 'none';
    statisticsView.style.display = 'none';
}

// 设置活动导航链接
function setActiveNavLink(link) {
    // 移除所有导航链接的活动类
    dashboardLink.classList.remove('active');
    usersLink.classList.remove('active');
    deliveryPersonsLink.classList.remove('active');
    ordersLink.classList.remove('active');
    statisticsLink.classList.remove('active');
    
    // 添加活动类到当前链接
    link.classList.add('active');
}

// 显示仪表盘视图
function showDashboardView() {
    hideAllViews();
    dashboardView.style.display = 'block';
    setActiveNavLink(dashboardLink);
    loadDashboardData();
}

// 显示用户视图
function showUsersView() {
    hideAllViews();
    usersView.style.display = 'block';
    setActiveNavLink(usersLink);
    loadUsersData();
}

// 显示配送员视图
function showDeliveryPersonsView() {
    hideAllViews();
    deliveryPersonsView.style.display = 'block';
    setActiveNavLink(deliveryPersonsLink);
    loadDeliveryPersonsData();
}

// 显示订单视图
function showOrdersView() {
    hideAllViews();
    ordersView.style.display = 'block';
    setActiveNavLink(ordersLink);
    loadOrdersData();
}

// 显示统计视图
function showStatisticsView() {
    hideAllViews();
    statisticsView.style.display = 'block';
    setActiveNavLink(statisticsLink);
    loadStatisticsData();
}

// 登出
function logout() {
    // 实现登出功能
    window.location.href = '/admin/login';
}

// 显示提示信息
function showAlert(message, type = 'info') {
    const alertContainer = document.getElementById('alertContainer');
    if (!alertContainer) return;
    
    const alertElement = document.createElement('div');
    alertElement.className = `alert alert-${type} alert-dismissible fade show`;
    alertElement.innerHTML = `
        ${message}
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    `;
    
    alertContainer.appendChild(alertElement);
    
    // 5秒后自动关闭
    setTimeout(() => {
        alertElement.classList.remove('show');
        setTimeout(() => alertElement.remove(), 150);
    }, 5000);
}

// 导出函数
window.initDashboard = initDashboard;
window.showDashboardView = showDashboardView;
window.showUsersView = showUsersView;
window.showDeliveryPersonsView = showDeliveryPersonsView;
window.showOrdersView = showOrdersView;
window.showStatisticsView = showStatisticsView;
window.logout = logout;
window.showAlert = showAlert;